/*
 * @Descripttion:
 * @version:
 * @Author: ada.wu
 * @Date: 2023-06-16 16:49:02
 * @LastEditors: ada.wu
 * @LastEditTime: 2023-10-26 21:27:27
 */
import { fileURLToPath, URL } from 'node:url'

import path from 'path'
import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import alias from "@rollup/plugin-alias";
// https://vitejs.dev/config/
export default defineConfig(({ command, mode }) => {
  // 获取当前模式下的环境配置（process.env.NODE_ENV 满足不了需求）
  const env = loadEnv(mode, './');
  console.log('env====',env);
  console.log('process==',process.env.NODE_ENV)

  return {
    plugins: [alias(),vue()],
    resolve: {
      alias: {
        // '@': fileURLToPath(new URL('./src', import.meta.url)),
        '@':  path.resolve(__dirname, 'src'),
      }
    },

    server: {
      port: env.VITE_PORT ?? 5101,

      proxy:{
        '/api':{
          // target:'http://192.168.2.248:8090',
          // target:'http://139.9.193.230:12350',
          target:'http://180.76.192.223:8091',
          // target:"http://192.168.2.3:5111",
          rewrite:(path) => path.replace(/^\/api/, '')
        },
      }
    },

    // 增加 base 选项，用于 Gitee Pages 基础地址，注意末尾要加斜杠
    base: env.VITE_ROUTER_BASE ?? '/',
    build: {
      chunkSizeWarningLimit: 1500,
      rollupOptions: {
        output: {
          manualChunks(id) {
            if (id.includes('node_modules')) {
              return id.toString().split('node_modules/')[1].split('/')[0].toString();
            }
          }
        }
      }
    },
    css: {
      // css预处理器
      preprocessorOptions: {
        less: {
          charset: false,
          additionalData: '@import "./src/assets/css/variable.less";',
        },
      },
    }
  }
})
